package com.ellwood.copyright.controller.softConf;

import com.ellwood.copyright.common.core.enums.HttpCodeEnum;
import com.ellwood.copyright.common.core.result.EllResult;
import com.ellwood.copyright.infrastructure.softConf.dto.TableInfoDTO;
import com.ellwood.copyright.modules.softConf.logic.TableValidateLogic;
import com.ellwood.copyright.modules.softConf.service.TableService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @ProjectName: ell-copyright-pro
 * @PackageName: com.ellwood.copyright.controller.softConf
 * @ClassName: TableController
 * @Description: // 数据表管理控制器
 * @Author: Ellwood
 * @CreateDate: 2024/11/19 - 11:19
 * @Version: V1.0.0
 */
@Slf4j
@Tag(name = "数据表管理")
@RestController
@RequestMapping("/api/soft/conf/table")
@AllArgsConstructor
public class TableController {

    private final TableService tableService;

    @Operation(summary = "创建数据表")
    @PostMapping("create")
    public EllResult<String> createTable(@RequestBody TableInfoDTO request) {
        try{
            // 验证输入
            TableValidateLogic.validateInput(request.getTableName(),request.getFields());
            tableService.executeSQL(request);
            return EllResult.ok(HttpCodeEnum.DATA_TABLE_CREATE_SUCCESS.getMessage());
        }catch (Exception e){
            log.error(e.getMessage());
            return EllResult.error(HttpCodeEnum.DATA_TABLE_CREATE_ERROE.getMessage());
        }
    }

}
